#include "tab_delimited_str.h"
#include <stdexcept>
#include <iostream>
#include "ultoa.h"
using std::string;
using std::istream;
#include <cstdio>



// returns the tab or newline which ended the field
// delimit is the character which may or may not precede the field
std::string read_char_delimited_str(istream& istrm, char initial_delimiter, char& end_delimiter)
{
	string data_str;
	
	char initial_whitespace = istrm.peek();
	// swallow EOF
	if (initial_whitespace == EOF)
	{
		initial_whitespace = istrm.get();
		end_delimiter = EOF;
		return data_str;
	}
	// if newline is first character of file, that will be swallowed. Make sure
	// line counting provision is made
	// force to swallow newline unless at beginning of file
	if (initial_whitespace == initial_delimiter)
		initial_whitespace = istrm.get();
	else
		if (0 != istrm.tellg())
			throw std::runtime_error("Unexpected field separator.");

	// read until EOF or tab
	while ((end_delimiter = istrm.peek()) != EOF && end_delimiter != '\n' && end_delimiter != '\t')
		data_str += istrm.get();
	return data_str;
}

